# import尝试段
import os
import re
import sys

count = 3
while count:
    try:
        from docx import Document
        from docx.oxml.ns import qn
        count = 3
        break
    except ImportError:
        print('docx模块未安装,现在准备开始安装...')
        os.system("pip install python-docx -i https://pypi.tuna.tsinghua.edu.cn/simple")
        count -= 1
        continue


def charChange(s):
    ss = s.replace("Ａ", "A")
    ss = ss.replace("Ｂ", "B")
    ss = ss.replace("Ｃ", "C")
    ss = ss.replace("Ｄ", "D")
    ss = ss.replace("Ｅ", "E")
    return ss


filename = ''
print(sys.argv, len(sys.argv))
if len(sys.argv) == 1:
    mode = 1
else:
    mode = 0

if mode:
    while 1:
        filename = input('文件路径->')
        if not os.path.exists(filename):
            print('文件不存在或文件路径出错，请再次尝试...')
        else:
            break
else:
    filename = sys.argv[1]
    if not os.path.exists(filename):
        print('文件不存在或文件路径出错，请再次尝试...')
        exit(0)

doc = Document(filename)
newDocx = Document()

rawd = []

for paragraph in doc.paragraphs:
    tmp = re.split('\n', charChange(paragraph.text))
    for i in tmp:
        rawd.append(i)

for s in rawd:
    newDocx.add_paragraph(s)
# print(rawd)
newDocx.styles['Normal'].font.name = u'宋体'
newDocx.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
newDocx.save('new'+filename)
print(f"转换成功 {filename} -> {'new'+filename}")
os.system('pause')
